package com.taptech.doufu.net.retrofit.api;

import android.content.Context;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.view.WaitDialog;

import java.lang.ref.WeakReference;

import rx.Subscriber;

/**
 * BaseSubscriber
 * author zhangyongcun
 * 16/12/9
 */


public class BaseSubscriber<T> extends Subscriber<T> {

    private static final String TAG = BaseSubscriber.class.getSimpleName();

    protected WaitDialog waitDialog;
    private WeakReference<Context> mActivity;


    public BaseSubscriber() {}

    /**
     * 传入 Context 则显示 Dialog 否则不显示
     * @param context
     */
    protected BaseSubscriber(Context context) {
        this.mActivity = new WeakReference<>(context);
        Context mContext = mActivity.get();
        waitDialog = new WaitDialog(mContext, R.style.updateDialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (waitDialog != null) {
            waitDialog.show();
        }
    }
    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
        if (waitDialog != null) {
            waitDialog.dismiss();
        }
        e.printStackTrace();
        unsubscribe();
    }

    @Override
    public void onNext(T t) {
        if (waitDialog != null) {
            waitDialog.dismiss();
        }
        unsubscribe();
    }
}
